using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QLThuHocPhi
{
    public partial class ListKhoa : Form
    {
        QLThuHocPhi.Database.DBTool dbt = new QLThuHocPhi.Database.DBTool();
        DataSet ds = new DataSet();
        public ListKhoa()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
           
            
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dbt.loadform(QLThuHocPhi.Database.DBTool.username);
            string tenvn = QLThuHocPhi.Database.DBTool.quyen;
            if (!tenvn.Equals("Admin"))
            {
                MessageBox.Show("Bạn không có quyền Admin");
                return;
            }
            if (MessageBox.Show("Bạn có chắc chắn muốn sửa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                dbt.UpdateKhoa(textBox3.Text, textBox1.Text);
            ds = dbt.GetListKhoa();
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void ListKhoa_Load(object sender, EventArgs e)
        {
            ds = dbt.GetListKhoa();
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.Columns[0].HeaderText = "Mã Khoa";
            dataGridView1.Columns[1].HeaderText = "Tên Khoa";            
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
               }

        private void button3_Click(object sender, EventArgs e)
        {
            dbt.loadform(QLThuHocPhi.Database.DBTool.username);
            string tenvn = QLThuHocPhi.Database.DBTool.quyen;
            if (!tenvn.Equals("Admin"))
            {
                MessageBox.Show("Bạn không có quyền Admin");
                return;
            }
            if (MessageBox.Show("Bạn có chắc chắn xóa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {

                int index = dataGridView1.CurrentRow.Index;

                if (index != -1)
                {
                    string k = index.ToString();
                    dbt.DeleteKhoa(textBox3.Text);
                    dataGridView1.Rows.RemoveAt(index);
                    ds = dbt.GetListKhoa();
                    dataGridView1.DataSource = ds.Tables[0];
                }            
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int i = dataGridView1.CurrentRow.Index;
                textBox1.Text = (string)dataGridView1.Rows[i].Cells["TenKhoa"].Value.ToString();
                textBox3.Text = (string)dataGridView1.Rows[i].Cells["MaKhoa"].Value.ToString();
            }
            catch (Exception ) { 
            
            }
        }

       
    }
}